package air.jason.com.porterduffxfermodetest.view;

/**
 * @DESC:
 * @Author: Jason
 * @Date: 16/8/25
 * @Time: 00:03
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

import air.jason.com.porterduffxfermodetest.R;

/**
 * PorterDuff.Mode.DST_OUT
 *          计算方式：[Da * (1 - Sa), Dc * (1 - Sa)]
 *          只在源图像和目标图像不相交的地方绘制目标图像
 */

public class DisOutView3 extends View{

    private Paint mPaint;
    private Bitmap mBitmapSrc; // 位图
    private PorterDuffXfermode mPorterDuffXfermode;

    private int x, y;
    private int screenW,screenH;

    public DisOutView3(Context context) {
        this(context, null);
    }

    public DisOutView3(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPorterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBitmapSrc = BitmapFactory.decodeResource(context.getResources(), R.drawable.a3_mask);

        // 获取屏幕尺寸
        screenW = context.getResources().getDisplayMetrics().widthPixels;
        screenH = context.getResources().getDisplayMetrics().heightPixels;

        x = screenW / 2 - mBitmapSrc.getWidth() / 2;
        y = screenH / 2 - mBitmapSrc.getHeight() / 2;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);

        int sc = canvas.saveLayer(0, 0, screenW, screenH, null, Canvas.ALL_SAVE_FLAG);

        // 先绘制外层颜色
        canvas.drawColor(0xFF8f66DA);

        // 设置混合模式
        mPaint.setXfermode(mPorterDuffXfermode);

        // 再绘制src源图
        canvas.drawBitmap(mBitmapSrc, x, y, mPaint);
        mPaint.setXfermode(null);
        canvas.restoreToCount(sc);

        // 绘制图片
    }
}
